import { defineStore } from 'pinia'
import type { RecordView } from '../types/RecordView'

/**
 * 记录查看存储模块
 * 负责管理领导查看记录的全局状态
 */
export const useRecordViewStore = defineStore('recordView', {
    state: () => ({
        // 所有查看记录
        viewRecords: [] as RecordView[],
        // 加载状态
        loading: false,
        // 筛选条件
        filter: {
            recordType: null as number | null,
            leaderId: null as number | null,
            dateRange: {
                start: null as string | null,
                end: null as string | null
            }
        }
    }),
    actions: {
        /**
         * 获取所有查看记录
         */
        async fetchAllViewRecords() {
            this.loading = true
            try {
                // this.viewRecords = await recordApi.getAllViewRecords()
            } finally {
                this.loading = false
            }
        },

        /**
         * 添加新的查看记录
         * @param newView - 新查看记录对象
         */
        async addViewRecord(newView: Omit<RecordView, 'viewId'>) {
            this.loading = true
            try {
                // const created = await recordApi.addViewRecord(newView)
                // this.viewRecords.unshift(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置记录类型筛选
         * @param type - 记录类型(1-巡课,2-巡寝)
         */
        setRecordTypeFilter(type: number | null) {
            this.filter.recordType = type
        },

        /**
         * 设置领导ID筛选
         * @param leaderId - 领导ID
         */
        setLeaderFilter(leaderId: number | null) {
            this.filter.leaderId = leaderId
        },

        /**
         * 设置日期范围筛选
         * @param start - 开始日期
         * @param end - 结束日期
         */
        setDateRange(start: string | null, end: string | null) {
            this.filter.dateRange = { start, end }
        }
    },
    getters: {
        /**
         * 获取筛选后的查看记录
         */
        filteredViewRecords(state): RecordView[] {
            let result = [...state.viewRecords]

            if (state.filter.recordType !== null) {
                result = result.filter(r => r.recordType === state.filter.recordType)
            }

            if (state.filter.leaderId !== null) {
                result = result.filter(r => r.leaderId === state.filter.leaderId)
            }

            if (state.filter.dateRange.start) {
                result = result.filter(r => r.viewTime >= state.filter.dateRange.start!)
            }

            if (state.filter.dateRange.end) {
                result = result.filter(r => r.viewTime <= state.filter.dateRange.end!)
            }

            return result.sort((a, b) =>
                new Date(b.viewTime).getTime() - new Date(a.viewTime).getTime()
            )
        },

        /**
         * 获取各领导的查看统计
         */
        leaderViewStats(state) {
            const stats = new Map<number, number>()
            state.viewRecords.forEach(record => {
                stats.set(record.leaderId, (stats.get(record.leaderId) || 0) + 1)
            })
            return Array.from(stats.entries()).map(([leaderId, count]) => ({
                leaderId,
                count
            }))
        }
    }
})
